<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="shortcut icon" href="../../../favicon.ico" type="image/x-icon">
	<title>life_cycle</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../../../js/react.development.js" ></script>
  <script type="text/javascript" src="../../../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../../../js/babel.min.js"></script>
  <script type="text/javascript" src="../../../js/prop-types.js"></script>

  <script type="text/babel">
    class LifeCycle extends React.Component {
      state = {
        opacity: 0.1
      }

      componentDidMount(){
        console.log('componentDidMount')
        this.timer = setInterval(()=>{
          let {opacity} = this.state
          opacity -= 0.1
          if(opacity<=0) opacity=1
          this.setState({opacity})
        }, 200)
      }

      componentWillUnmount(){
        console.log('componentWillUnmount')
        clearInterval(this.timer)
      }

      dead=()=>{
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      render(){
        console.log('render')
        return (
          <div>
            <h2 style={{opacity: this.state.opacity}}>人生模拟器</h2>
            <button onClick={this.dead}>走到尽头</button>
          </div>
        )
      }
    }

    ReactDOM.render(<LifeCycle />, document.getElementById('test'));
  </script>
</body>